/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ProdLang;
import com.yami.shop.bean.model.Product;
import com.yami.shop.dao.ProdLangMapper;
import com.yami.shop.dao.ProductMapper;
import com.yami.shop.dao.SkuLangMapper;
import com.yami.shop.dao.SkuMapper;
import com.yami.shop.service.ProdLangService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 *
 * @author lhd
 * @date 2020-08-26 15:39:09
 */
@Service
@AllArgsConstructor
public class ProdLangServiceImpl extends ServiceImpl<ProdLangMapper, ProdLang> implements ProdLangService {

    private final ProdLangMapper prodLangMapper;
    private final SkuMapper skuMapper;
    private final SkuLangMapper skuLangMapper;
    private final ProductMapper productMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {
            @CacheEvict(cacheNames = "product", key = "#prodId"),
            @CacheEvict(cacheNames = "skuList", key = "#prodId")
    })
    public void removeProductAndSkuByProdId(Long prodId) {
        // 将商品的状态变成-1（逻辑删除）
        Product product = new Product();
        product.setProdId(prodId);
        product.setIsTop(0);
        product.setStatus(-1);
        productMapper.updateById(product);
        skuMapper.deleteByProdId(prodId);
        // 删除语言表信息
        remove(new LambdaUpdateWrapper<ProdLang>().eq(ProdLang::getProdId,prodId));
        skuLangMapper.deleteByProdId(prodId);
    }
}
